package com.example.converter;
import com.alibaba.dubbo.common.json.JSON;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


public class StringMessageConverter extends org.springframework.http.converter.StringHttpMessageConverter {
    @Value("${code_key:code}")
    private String codeKey;

    @Value("${message_key:message}")
    private String messageKey;

    @Value("${data_key:data}")
    private String data;

    @Override
    protected void writeInternal(String str, HttpOutputMessage outputMessage) throws IOException {
        Map<String, Object> returnValue = new HashMap<>();
        returnValue.put(data, str);
        returnValue.put(codeKey, "0000");
        returnValue.put(messageKey, "操作成功");
        super.writeInternal(JSON.json(returnValue), outputMessage);
       // super.writeInternal("1234slj;gk;kh", outputMessage);
        //!!!重大的消息，不能用这些util，要用JSON.json(),屏蔽了可能的"试试
        //为什么解析string这边出错了，见鬼了，为什么会返回这个{"code":"0  尼玛长度之后这几个？
        //返回的长度是本来应该返回的字符的长度
        //项目组的controller都是返回的json，没看到返回string的好像，返回string的都是服务提供者
    }
}